package cn.boodqian.d3surveyor;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import cn.boodqian.util.Log;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class D3SurveyorDBHelper {

	public static String mDatabasePath = android.os.Environment
			.getExternalStorageDirectory().getAbsolutePath() + "/D3Surveyor";
	public static String mDatabaseName = "d3surveyor.sqlite";
	public static int mDBVersion = 1;
	Context mContext;
	private SQLiteDatabase mDB = null;
	private int mVersion;
	
	public D3SurveyorDBHelper(Context context, int version) {
		assert(context!=null);
		mContext = context;
		mVersion = version;
		
		Log.i(mDatabasePath + "/" + mDatabaseName + ":"+ (new File(mDatabasePath + "/" + mDatabaseName)).exists());
	}
	
	public D3SurveyorDBHelper(Context context) {
		this(context,  mDBVersion);
	}
	
	public SQLiteDatabase getReadableDatabase() {
		if(mDB!=null && mDB.isOpen()) return mDB;
		
		File db = new File(mDatabasePath, mDatabaseName);
		if(!db.exists()) onCreate();
		openDatabase();
		
		int version = mDB.getVersion();
		Log.i(String.format("DB version current=%d request=%d", version, mVersion));
		if(version < mVersion) {
			mDB.close();
			onUpgrade(version, mVersion);
			openDatabase();
		}
		
		return mDB;
	}

	public void onCreate() {
		File db = new File(mDatabasePath, mDatabaseName);
		copyDatabase();
	}

	public void onUpgrade(int oldVersion, int newVersion) {
		copyDatabase();
		return;
	}
	
	private void openDatabase() {
		mDB = mContext.openOrCreateDatabase(mDatabasePath + "/" + mDatabaseName, 0, null);
	}

	public boolean copyDatabase() {
		//String dbPath = mDatabasePath + "/" + mDatabaseName;
		File dir = new File(mDatabasePath, mDatabaseName);
		if (!dir.exists())
			if(!dir.mkdirs()) Log.i("mkdirs failed");
		else
			dir.delete();

		Log.i("Copy database begin");
		try {
			InputStream inputStream = mContext.getResources()
					.openRawResource(R.raw.d3surveyor);
			Log.i(dir.toString()+":"+dir.exists());
			FileOutputStream output = new FileOutputStream(dir);
			ZipInputStream zipInputStream = new ZipInputStream(
					new BufferedInputStream(inputStream));

			ZipEntry zipEntry = zipInputStream.getNextEntry();
			BufferedInputStream b = new BufferedInputStream(zipInputStream);

			int streamLen = (int) zipEntry.getSize();
			int readCount;
			int readSum = 0;

			byte[] buffer = new byte[8192];
			while ((readCount = b.read(buffer)) > 0) {
				output.write(buffer, 0, readCount);
				readSum = readSum + readCount;
			}
			Log.i("Read sum="+readSum);
			output.flush();
			output.close();
			inputStream.close();

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		Log.i("Copy database done");
		return true;
	}
}
